package com.aat.dxfy.mongo;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoDBUtil {

	public static void main(String[] args) throws Exception {
		// MongoClient mc = new MongoClient("127.0.0.1", 27017);
		// MongoClient mc=new
		// MongoClient("dds-2zec15414209d9842.mongodb.rds.aliyuncs.com", 3717);

		MongoClient mc = new MongoClient("dds-2zec15414209d9842.mongodb.rds.aliyuncs.com", 3717);
		DB db = mc.getDB("test");

		// 查询该库中所有的集合 (相当于表)
		for (String name : db.getCollectionNames()) {
			System.out.println("Collection Name: " + name);
		}

		DBCollection users = db.getCollection("users");
		// 查询集合中所有的数据
		DBCursor cur = users.find();
		System.out.println("Record Count:" + cur.count());
		while (cur.hasNext()) {
			DBObject object = cur.next();
			System.out.println(object);
			// 取出对象中列表为字段名为'uname'和'upwd'的数据
			System.out.println("uname:" + object.get("uname") + "\tupwd:" + object.get("upwd"));
		}
		mc.close();
	}

}
